var http = require('http');
var fs = require('fs');
var template = require('art-template');

var server = http.createServer();

var wwwDir = './resource';
server.on('request', function (req, res) {
	fs.readFile('./03-template.html', function (err, data) {
		if (err) {
			return res.end('404 Not Found.');
		}

		fs.readdir(wwwDir, function (err, files) {
			if (err) {
				return res.end(`Can not find ${wwwDir}`);
			}
			data = template.render(data.toString(), {
				title: '哈哈',
				files,
			});
			//发送解析替换后的响应数据
			res.end(data);
		});
	});
});

server.listen(3000, function () {
	console.log('Server is running...');
});
